﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using LitJson;

namespace JsonEditor
{
    public class ExportToJson : Editor
    {
        [MenuItem("GameObject/ExportJson")]
        static void ExportJSON()
        {
            string filepath = @".\..\code\bin\res\url\lvConfig\level.json";
            //filepath = @"C:\Users\EDZ\Desktop\LayaAir3D\huabingnvhai\New Unity Project\Assets\level.json";
            createFile(filepath);
            Object[] objAll = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
            GameObject[] gos = (GameObject[])FindObjectsOfType(typeof(GameObject));
            List<GameObject> allPrefab = new List<GameObject>();
            for (int i = 0; i < objAll.Length; i++)
            {
                GameObject obj = objAll[i] as GameObject;
                if (!obj.activeSelf)
                    continue;
                // if (obj.GetComponent<Light>() != null)
                //     continue;
                allPrefab.Add(obj);
            }

            Dictionary<string, List<Child>> childDic = new Dictionary<string, List<Child>>();
            List<Child> child = new List<Child>();
            foreach (var item in allPrefab)
            {
                if (!item.activeSelf)
                    continue;
                child.Add( SetChild(item.transform, (item.GetComponent<Camera>() != null) ,(item.GetComponent<Light>() != null)));
            }
            childDic.Add("root", child);
            File.WriteAllText(filepath, JsonMapper.ToJson(childDic));
            AssetDatabase.Refresh();
            Debug.Log("导出配置完成");
        }

        public static Child SetChild(Transform tmp, bool camera = false, bool light = false)
        {
            Vector3 pos = tmp.localPosition;
            Vector3 rot = tmp.localRotation.eulerAngles;
            Vector3 scale = tmp.localScale;
            Child child = new Child();
            child.SetNode(tmp);
            child.name = tmp.name;
            child.SetPos(pos);
            child.SetEulerAngles(rot);
            child.SetScale(scale);
            if(camera){
                child.SetCameraEulerAngles(rot);
                child.name = "camera";
            }
            if(light){
                child.SetCameraEulerAngles(rot);
                child.name = "light";
            }
            return child;
        }

        static void createFile(string filepath)
        {
            FileInfo t = new FileInfo(filepath);
            Debug.Log(filepath);
            if (!File.Exists(filepath))
            {
                File.Delete(filepath);
            }
        }

        public static bool IsPrefabAsset(UnityEngine.Object obj, bool includePrefabInstance)
        {
    #if UNITY_EDITOR
            if (!obj)
            {
                return false;
            }

            var type = UnityEditor.PrefabUtility.GetPrefabAssetType(obj);
            if (type == UnityEditor.PrefabAssetType.NotAPrefab)
            {
                return false;
            }

            var status = UnityEditor.PrefabUtility.GetPrefabInstanceStatus(obj);
            if (status != UnityEditor.PrefabInstanceStatus.NotAPrefab && !includePrefabInstance)
            {
                return false;
            }

            return true;
    #else
        return false;
    #endif
        }
    }

    public class Child
    {
        private Transform nodeTran;
        public string name;
        public string prefabName;
        public string position;

        public string euler;

        public string scale;
        public List<Child> child;

        public void SetNode(Transform node)
        {
            if(ExportToJson.IsPrefabAsset(node, true))
            {
                string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(node);
                GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                prefabName = go.name;
                return;
            }
            nodeTran = node;
            for (int i = 0; i < nodeTran.childCount; i++)
            {
                Transform tmp = nodeTran.GetChild(i);
                if(child == null)
                {
                    child = new List<Child>();
                }
                child.Add (ExportToJson.SetChild(tmp));
            }
        }
        
        public void SetPos(Vector3 pos)
        {
            position = (-pos.x) + "," + pos.y + "," + pos.z;
        }

        public void SetEulerAngles(Vector3 euler)
        {
            this.euler = euler.x + "," + (-euler.y) + "," + (-euler.z);
        }

        public void SetCameraEulerAngles(Vector3 euler)
        {
            this.euler = (-euler.x) + "," + (180-euler.y) + "," + euler.z;
        }

        public void SetScale(Vector3 scale)
        {
            this.scale = scale.x + "," + scale.y + "," + scale.z;
        }
    }
}
